layui.define(['jquery', 'layer'], function (exports) {
    "use strict";

    let $ = layui.jquery;
    let layer = layui.layer;
    let MOD_NAME = 'http';
    let context = new function () {

        this.SendAjax = function (url, data = [], headers = [], callback) {
            layer.load(1);
            let token = localStorage.getItem('token') || '';
            if (token) {
                headers.token = token;
            }
            let async=true;
            if (data.async)
            {
                async=false;
            }
            $.ajax({
                url: url,
                type: 'post',
                dataType: 'json',
                data: data,
                headers: headers,
                async:async,
                success: function (res) {
                    layui.layer.closeAll('loading');
                    var code = res.code || 0;
                    var data = res.data || [];
                    var token = data.token || '';
                    if (token) {
                        localStorage.setItem("token", token);
                    }
                    switch (Number(code)) {
                        case 201:
                            layer.confirm('系统监测到您还没有登录', function (index) {
                                window.open("/admin/index/login/",'_top')
                            })
                            break;
                        default:
                            callback(res)
                    }
                },
                error: function (e) {
                    layui.layer.closeAll('loading');
                    layer.msg('操作失败,请联系技术人员', {icon: 2});
                }
            });
        }
    }
    exports(MOD_NAME, context);
});